import 'dart:convert';

import 'package:aqua_plunge/http/Url.dart';
import 'package:aqua_plunge/model/media.dart';
import 'package:aqua_plunge/util/header_util.dart';
import 'package:http/http.dart' as http;

class MediaDao {
  static Utf8Decoder utf8decoder = const Utf8Decoder();

  ///获取 oss资源视频列表
  static Future<List<Media>> getvideolist(List<String> videolist) async {
    List<Media> datas = [];
    var httpurl = Uri.parse(AppUrl.getOssResource);

    List<mediaItem> datalist = [];
    for (var element in videolist) {
        datalist.add(mediaItem(mediaPath: element,mediaType: "video"));
    }
    var jsonStr = json.encode(datalist);
    try {
      var response = await http.post(httpurl,
          headers: await HeaderUtil().getHeader(),
          body: jsonStr,
          encoding: const Utf8Codec());
      if (response.statusCode == 200) {
        var result = json.decode(utf8decoder.convert(response.bodyBytes));
        if(result['data']!= null){
          var items = result['data'] as List<dynamic>;
          datas = items.map((json) => Media.fromJson(json)).toList();
        }
      } else {
        throw Exception('"Request failed with status: ${response.statusCode}"');
      }
    } catch (e) {
      Future.error(e);
    }
    return datas;
  }

}

class mediaItem {
  String? mediaPath;
  String? mediaType;

  mediaItem({this.mediaPath, this.mediaType});

  mediaItem.fromJson(Map<String, dynamic> json) {
    mediaPath = json['mediaPath'];
    mediaType = json['mediaType'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['mediaPath'] = this.mediaPath;
    data['mediaType'] = this.mediaType;
    return data;
  }
}



